// pages/createTeam/createTeam.js
var utils = require('../../utils/util.js');
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    form_teamname_class: "",
    form_date_class: "",
    form_area_class: "",
    avatar:'../../images/photo_img.png',
    logo_class:'',
    teamName:'',
    date:'',
    nowdate:'',
    address:''
  },
  /**
  * input 获取焦点事件
  */
  watchInputfocus: function (event) {

    var id = event.currentTarget.id
    if (id == "input_teamname") {
      this.setData({
        form_teamname_class: "form-control-focus"
      });
    } else if (id == "input_date") {
      this.setData({
        form_date_class: "form-control-focus"
      });
    } else if (id == "input_area") {
      this.setData({
        form_area_class: "form-control-focus"
      });
    }
  },
  /**
  * input 离开焦点事件
  */
  watchInputblur: function (event) {
    var id = event.currentTarget.id

    if (id == "input_teamname" && event.detail.value == "") {
      this.setData({
        form_teamname_class: ""
      });
    } else if (id == "input_teamname" && event.detail.value != "") {
      this.setData({
        form_teamname_class: "form-control-focus-gray"
      });
    }

    if (id == "input_date" && event.detail.value == "") {
      this.setData({
        form_date_class: ""
      });
    } else if (id == "input_date" && event.detail.value != "") {
      this.setData({
        form_date_class: "form-control-focus-gray"
      });
    }

    if (id == "input_area" && event.detail.value == "") {
      this.setData({
        form_area_class: ""
      });
    } else if (id == "input_area" && event.detail.value != "") {
      this.setData({
        form_area_class: "form-control-focus-gray"
      });
    }

  },
  // 上传logo
  changeAvatar:function(){

    var that = this;
    // var childId = wx.getStorageSync("child_id");
    var token = wx.getStorageSync('token');

    wx.chooseImage({
      count: 1, // 最多可以选择的图片张数，默认9
      sizeType: ['compressed'], // original 原图，compressed 压缩图，默认二者都有
      sourceType: ['album', 'camera'], // album 从相册选图，camera 使用相机，默认二者都有
      success: function (res) {
        var avatar = res.tempFilePaths;
        wx.uploadFile({
          url: app.globalData.httpsPath+'/upload',
          method: "POST",
          filePath: avatar[0],
          name: 'pic',
          header: {
            "content-type": "multipart/form-data"
          },
          formData: {
            //和服务器约定的token, 一般也可以放在header中
            'token': token
          },
          success: function (res) {
            var _imgdata = JSON.parse(res.data);
            if (res.statusCode != 200) {
              wx.showModal({
                title: '提示',
                content: '上传失败',
                showCancel: false
              })
              return;
            }
            var _imgpath = _imgdata.data
            that.setData({  //上传成功修改显示头像
              avatar: app.globalData.imagePath + _imgpath
            })
          },
          fail: function (e) {
            wx.showModal({
              title: '提示',
              content: '上传失败',
              showCancel: false
            })
          },
          complete: function () {
            wx.hideToast();  //隐藏Toast
          }
        });
      }
    })
  },
  // 球队名称
  teamnameInput: function (event){
    this.setData({
      teamName: event.detail.value
    })
  },
  //日期
  bindDateChange: function(event) {
    this.setData({
      date: event.detail.value
    })
  },
  //所在地区
  areaInput:function(event){
    this.setData({
      address: event.detail.value
    })
  },
  //保存数据
  creationTeam:function(){
    var that = this;

    if (that.data.avatar == '../../images/photo_img.png') {
      this.setData({
        logo_class: "error-create-text"
      });
      return false;
    }
    if (that.data.teamName == '') {
      this.setData({
        form_teamname_class: "form-control-error"
      });
      return false;
    }
    if (that.data.date == '') {
      this.setData({
        form_date_class: "form-control-error"
      });
      return false;
    }
    var _token = wx.getStorageSync("token");

    var _imgpath = that.data.avatar.split("cn/");
    wx.request({
      url: app.globalData.httpsPath +'/team/save', // 接口地址
      method: "POST",
      header: {
        'content-type': 'application/json', // 默认值
        // 'token': _token
      },
      data: {
        token: _token,
        name: that.data.teamName,
        logo: _imgpath[1],
        since_year: that.data.date
      },
      success(res) {
        wx.showToast({
          title: '注册成功',
          icon: 'succes',
          duration: 3000,
          mask: true
        })
        setTimeout(function () {
          wx.reLaunch({
            url: '../personalCenter/personalCenter'
          })
        }, 3000)

      }
    }) 
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (e) {
    var _DATE = utils.formatDate(new Date());
    var token = wx.getStorageSync("token");
    this.setData({
      token: token,
      nowdate: _DATE
    })
  },

})